package com.learning.railwayreservation.dao;

import java.sql.CallableStatement;
import java.sql.Connection;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import oracle.jdbc.OracleCallableStatement;
import oracle.jdbc.OracleTypes;

import com.learning.railwayreservation.vo.BookingInputVO;
import com.learning.railwayreservation.vo.BookingOutputVO;
import com.learning.railwayreservation.vo.PassengerVO;
import com.learning.railwayreservation.vo.TicketVO;

public class BookingDao extends BaseDao {
	public static void main(String[] args) {

	}

	
	public TicketVO bookTickets(TicketVO ticketvo) {
		try {
			DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		BookingDao tdao = new BookingDao();
		Connection conn = tdao.getConnection();
		
		List<PassengerVO> passengerList = ticketvo.getPassengerList();
		
		List<PassengerVO> passengerOutputList = new ArrayList<PassengerVO>();

		try {

			 CallableStatement stmt = conn.prepareCall("{? = call railway_reservation.book_seats(?,?,?,?,?)}");
			for (PassengerVO passVo : passengerList) {
				stmt.setInt(2, passVo.getSeatVo().getUniqueSeatId());
				stmt.setString(3, passVo.getFirstName());
				stmt.setString(4, passVo.getLastName());
				stmt.setInt(5, passVo.getAge());
				stmt.setString(6, passVo.getSex());
				stmt.registerOutParameter(1, OracleTypes.CURSOR); // REF CURSOR
				//stmt.addBatch();
				stmt.execute();
				ResultSet rs = (ResultSet) ((OracleCallableStatement) stmt)
						.getObject(1);

				while (rs.next()) {
					passVo.setFirstName(rs.getString("FIRST_NAME"));
					passVo.setLastName(rs.getString("LAST_NAME"));
					passVo.setAge(rs.getInt("AGE"));
					passVo.setSex(rs.getString("SEX"));
					passVo.getSeatVo().setSeatId(rs.getInt("SEAT_ID"));
					passVo.getSeatVo().setUniqueSeatId(rs.getInt("S_ID"));
					passVo.getSeatVo().setRowId(rs.getString("ROW_ID"));
					passVo.getSeatVo().setBoggieId(rs.getInt("B_ID"));
					ticketvo.setBoggieClass(rs.getString("BOGGIE_TYPE"));
					ticketvo.setTrainId(rs.getInt("T_ID"));
					ticketvo.setTrainName(rs.getString("TRAIN_NAME"));
//					ticketvo.setArrivalTime(rs.getString("ARRIVAL_TIME"));
					ticketvo.setArrivalTime(rs.getString("START_DATE"));
//					ticketvo.setArrivalTo(rs.getString("ARRIVAL_TO"));
//					ticketvo.setDepartureFrom(rs.getString("DEPARTURE_FROM"));
					
					passengerOutputList.add(passVo);

				}
				ticketvo.setPassengerList(passengerOutputList);
			}

			//stmt.registerOutParameter(6, OracleTypes.CURSOR); // REF CURSOR
						// System.out.println(passengerName);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return ticketvo;
	}

}
